package LeetCode.LeetCode2;

/**
 * 反转字符串中的单词
 * @author lumin
 * @date 2022/03/22 23:09
 **/

public class Num557 {
    public String reverseWords(String s) {
        char[] arr = s.toCharArray();
        int l = 0;
        int r = 0;
        while(r < arr.length){
            if(arr[r] == ' '){
                reverse(arr,l,r);
                l = r + 1;
            }
            r++;
        }
        reverse(arr,l,r);
        String str = new String(arr);
        return str;
    }

    private void reverse(char[] arr, int l, int r) {
        while(l < r){
            char temp = arr[l];
            arr[l] = arr[r - 1];
            arr[r - 1] = temp;
            l++;
            r--;
        }
    }
}